package Lobby;

import jHaggis.jTransPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import GameModel.mGame;
import Player.mPlayer;

/**
 * Generiert eine Gameroom-Komponente in der Lobby, welche erlaubt, Spielern
 * einem Spiel beizutreten.
 * 
 * @author Christopher Vogel
 */
public class jGameRoom extends JButton implements MouseListener {

	// private volatile boolean finished = false;
	private mGame Game;
	private ArrayList<mPlayer> PlayerInGameList;
	private JLabel lblGameName = new JLabel();
	private jTransPanel pnlContent = new jTransPanel(new FlowLayout(
			FlowLayout.LEFT));
	private JLabel lblPlayers = new JLabel();
	private JLabel lblPlayerNumbers = new JLabel();
	private JLabel lblPoints = new JLabel();
	protected JLabel btnJoin = new JLabel(" join ");

	jGameRoom(mGame Game) {
		super.setBorder(BorderFactory.createLineBorder(new Color(255, 255, 255,
				30)));
		JPanel wrapper = new jTransPanel(new BorderLayout());

		this.setPreferredSize(new Dimension(800, 55));
		this.setLayout(new BorderLayout());
		wrapper.setBorder(new EmptyBorder(2, 4, 2, 2));
		wrapper.setLayout(new BorderLayout());
		wrapper.setPreferredSize(new Dimension(790, 40));
		this.add(wrapper);

		this.setOpaque(false);

		this.Game = Game;
		this.PlayerInGameList = Game.getPlayers();

		Font myFont = new Font("Arial", Font.BOLD, 20);

		wrapper.add(lblGameName, BorderLayout.NORTH);

		lblGameName.setFont(myFont);
		lblGameName.setForeground(Color.WHITE);
		wrapper.add(pnlContent);

		pnlContent.add(lblPlayers);
		lblPlayers.setForeground(Color.WHITE);
		lblPlayers.setPreferredSize(new Dimension(300, 20));

		pnlContent.add(lblPlayerNumbers);
		lblPlayerNumbers.setForeground(Color.WHITE);
		lblPlayerNumbers.setPreferredSize(new Dimension(250, 20));
		pnlContent.add(lblPoints);

		lblPoints.setForeground(Color.WHITE);
		btnJoin.setForeground(Color.WHITE);
		btnJoin.setBorder(BorderFactory.createLineBorder(Color.white));
		wrapper.add(btnJoin, BorderLayout.EAST);
		this.updateLabels();

		this.setBackground(new Color(255, 255, 255, 0));

		this.addMouseListener(this);
		this.btnJoin.addMouseListener(this);
	}

	/**
	 * Aktualisierte alle Informationen eines Gamerooms
	 * 
	 * @author Christopher Vogel
	 * @return void
	 */
	private void updateLabels() {
		lblGameName.setText(Game.getGameName());
		lblPlayers.setText(PlayerInGameList.toString().substring(1,
				PlayerInGameList.toString().length() - 1));
		lblPlayerNumbers.setText("Players waiting " + PlayerInGameList.size()
				+ "/" + Game.getPlayerNumber());

		lblPoints.setText("Points: "
				+ new Integer(Game.getMaxPoints()).toString());
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		g2d.setColor(this.getBackground());
		g2d.fillRect(0, 0, getWidth(), getHeight());
		super.paintComponent(g2d);
		this.setOpaque(false);
		g2d.dispose();
		this.repaint();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// this.btnJoin.doClick();

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// if (this.equals(arg0)) {
		this.setBackground(new Color(255, 255, 255, 50));
		// }
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// if (this.equals(arg0)) {
		this.setBackground(new Color(255, 255, 255, 0));
		this.setOpaque(false);
		// }
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		this.setBackground(new Color(255, 255, 255, 100));

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		this.setBackground(new Color(255, 255, 255, 50));
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((Game == null) ? 0 : Game.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		jGameRoom other = (jGameRoom) obj;
		if (Game == null) {
			if (other.Game != null)
				return false;
		} else if (!Game.equals(other.Game))
			return false;
		return true;
	}

	public mGame getGame() {
		return Game;
	}

}
